package com.example.webservice.provider.service;


import com.example.webservice.provider.bean.User;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import java.util.HashMap;

@WebService(serviceName = "userManager",// 修改服务名
        targetNamespace = "http://service.provider.webservice.example.com") // 修改命名空间，默认包名取反
public class UserService {

    private static final HashMap<Integer, User> USER_MAP = new HashMap<>();

    static {
        USER_MAP.put(1, new User(1, "tom", 11));
        USER_MAP.put(2, new User(2, "lucy", 12));
        USER_MAP.put(3, new User(3, "jack", 13));
        USER_MAP.put(4, new User(4, "mary", 14));
    }

    public static void main(String[] args) {
        String address = "http://127.0.0.1:8888/ws/userService";
        /*
         * 发布webservice服务
         * 1.address：服务的地址
         * 2：implementor 服务的实现对象
         */
        Endpoint.publish(address, new UserService());
        System.out.println("wsdl地址: " + address + "?WSDL");
    }

    public User addUser(User user) {
        Integer max = USER_MAP.keySet().stream().max(Integer::compareTo).orElse(0);
        Integer id = max + 1;
        user.setId(id);
        USER_MAP.put(id, user);
        return user;
    }

    public User getUser(Integer id) {
        return USER_MAP.get(id);
    }
}

